| Conditions | 1 |
| Paths | 1 |
| Total Lines | 191 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const GitAmp = (function(exports, $) { |
||
| 168 | const Gui = (function() { |
||
| 169 | const scaleFactor = 6; |
||
| 170 | const textColor = '#ffffff'; |
||
| 171 | const maxLife = 20000; |
||
| 172 | |||
| 173 | function Event(event, svg) { |
||
| 174 | this.event = event; |
||
| 175 | this.svg = svg; |
||
| 176 | } |
||
| 177 | |||
| 178 | Event.prototype.getSize = function() { |
||
| 179 | return Math.max(Math.sqrt(Math.abs(this.event.getMessage().length)) * scaleFactor, 3); |
||
| 180 | }; |
||
| 181 | |||
| 182 | Event.prototype.getText = function() { |
||
| 183 | switch(this.event.getType()){ |
||
| 184 | case 'PushEvent': |
||
| 185 | return this.event.getActorName() + ' pushed to ' + this.event.getRepositoryName(); |
||
| 186 | case 'PullRequestEvent': |
||
| 187 | return this.event.getActorName() + ' ' + this.event.getAction() + ' ' + ' a PR for ' + this.event.getRepositoryName(); |
||
| 188 | case 'IssuesEvent': |
||
| 189 | return this.event.getActorName() + ' ' + this.event.getAction() + ' an issue in ' + this.event.getRepositoryName(); |
||
| 190 | case 'IssueCommentEvent': |
||
| 191 | return this.event.getActorName() + ' commented in ' + this.event.getRepositoryName(); |
||
| 192 | case 'ForkEvent': |
||
| 193 | return this.event.getActorName() + ' forked ' + this.event.getRepositoryName(); |
||
| 194 | case 'CreateEvent': |
||
| 195 | return this.event.getActorName() + ' created ' + this.event.getRepositoryName(); |
||
| 196 | case 'WatchEvent': |
||
| 197 | return this.event.getActorName() + ' watched ' + this.event.getRepositoryName(); |
||
| 198 | } |
||
| 199 | }; |
||
| 200 | |||
| 201 | Event.prototype.getBackgroundColor = function() { |
||
| 202 | switch(this.event.getType()){ |
||
| 203 | case 'PushEvent': |
||
| 204 | return '#22B65D'; |
||
| 205 | case 'PullRequestEvent': |
||
| 206 | return '#8F19BB'; |
||
| 207 | case 'IssuesEvent': |
||
| 208 | return '#ADD913'; |
||
| 209 | case 'IssueCommentEvent': |
||
| 210 | return '#FF4901'; |
||
| 211 | case 'ForkEvent': |
||
| 212 | return '#0184FF'; |
||
| 213 | case 'CreateEvent': |
||
| 214 | return '#00C0C0'; |
||
| 215 | case 'WatchEvent': |
||
| 216 | return '#E60062'; |
||
| 217 | } |
||
| 218 | }; |
||
| 219 | |||
| 220 | Event.prototype.getRingAnimationDuration = function() { |
||
| 221 | if (this.event.getType() === 'PullRequestEvent') { |
||
| 222 | return 10000; |
||
| 223 | } |
||
| 224 | |||
| 225 | return 3000; |
||
| 226 | }; |
||
| 227 | |||
| 228 | Event.prototype.getRingRadius = function() { |
||
| 229 | if (this.event.getType() === 'PullRequestEvent') { |
||
| 230 | return 600; |
||
| 231 | } |
||
| 232 | |||
| 233 | return 80; |
||
| 234 | }; |
||
| 235 | |||
| 236 | Event.prototype.draw = function(width, height) { |
||
| 237 | let no_label = false; |
||
| 238 | let size = this.getSize(); |
||
| 239 | |||
| 240 | const self = this; |
||
| 241 | |||
| 242 | //noinspection JSUnresolvedFunction |
||
| 243 | Math.seedrandom(this.event.getMessage()); |
||
| 244 | let x = Math.random() * (width - size) + size; |
||
| 245 | let y = Math.random() * (height - size) + size; |
||
| 246 | |||
| 247 | let circle_group = this.svg.append('g') |
||
| 248 | .attr('transform', 'translate(' + x + ', ' + y + ')') |
||
| 249 | .attr('fill', this.getBackgroundColor()) |
||
| 250 | .style('opacity', 1); |
||
| 251 | |||
| 252 | let ring = circle_group.append('circle'); |
||
| 253 | ring.attr({r: size, stroke: 'none'}); |
||
| 254 | ring.transition() |
||
| 255 | .attr('r', size + this.getRingRadius()) |
||
| 256 | .style('opacity', 0) |
||
| 257 | .ease(Math.sqrt) |
||
| 258 | .duration(this.getRingAnimationDuration()) |
||
| 259 | .remove(); |
||
| 260 | |||
| 261 | let circle_container = circle_group.append('a'); |
||
| 262 | circle_container.attr('xlink:href', this.event.getUrl()); |
||
| 263 | circle_container.attr('target', '_blank'); |
||
| 264 | circle_container.attr('fill', textColor); |
||
| 265 | |||
| 266 | let circle = circle_container.append('circle'); |
||
| 267 | circle.classed(this.event.getType(), true); |
||
| 268 | circle.attr('r', size) |
||
| 269 | .attr('fill', this.getBackgroundColor()) |
||
| 270 | .transition() |
||
| 271 | .duration(maxLife) |
||
| 272 | .style('opacity', 0) |
||
| 273 | .remove(); |
||
| 274 | |||
| 275 | circle_container.on('mouseover', function() { |
||
| 276 | circle_container.append('text') |
||
| 277 | .text(self.getText()) |
||
| 278 | .classed('label', true) |
||
| 279 | .attr('text-anchor', 'middle') |
||
| 280 | .attr('font-size', '0.8em') |
||
| 281 | .transition() |
||
| 282 | .delay(1000) |
||
| 283 | .style('opacity', 0) |
||
| 284 | .duration(2000) |
||
| 285 | .each(function() { no_label = true; }) |
||
| 286 | .remove(); |
||
| 287 | }); |
||
| 288 | |||
| 289 | circle_container.append('text') |
||
| 290 | .text(this.getText()) |
||
| 291 | .classed('article-label', true) |
||
| 292 | .attr('text-anchor', 'middle') |
||
| 293 | .attr('font-size', '0.8em') |
||
| 294 | .transition() |
||
| 295 | .delay(2000) |
||
| 296 | .style('opacity', 0) |
||
| 297 | .duration(5000) |
||
| 298 | .each(function() { no_label = true; }) |
||
| 299 | .remove(); |
||
| 300 | }; |
||
| 301 | |||
| 302 | function Gui() { |
||
| 303 | //noinspection JSUnresolvedVariable |
||
| 304 | this.svg = exports.d3.select('#area').append('svg'); |
||
| 305 | |||
| 306 | exports.addEventListener('resize', this.resize.bind(this)); |
||
| 307 | |||
| 308 | this.setupVolumeSlider(); |
||
| 309 | this.resize(); |
||
| 310 | } |
||
| 311 | |||
| 312 | Gui.prototype.setupVolumeSlider = function() { |
||
| 313 | //noinspection JSUnresolvedFunction |
||
| 314 | $('#volumeSlider').slider({ |
||
| 315 | max: 100, |
||
| 316 | min: 0, |
||
| 317 | value: volume * 100, |
||
| 318 | slide: function (event, ui) { |
||
| 319 | //noinspection JSUnresolvedVariable |
||
| 320 | exports.Howler.volume(ui.value/100.0); |
||
| 321 | }, |
||
| 322 | change: function (event, ui) { |
||
| 323 | //noinspection JSUnresolvedVariable |
||
| 324 | exports.Howler.volume(ui.value/100.0); |
||
| 325 | } |
||
| 326 | }); |
||
| 327 | }; |
||
| 328 | |||
| 329 | Gui.prototype.getWidth = function() { |
||
| 330 | return exports.innerWidth; |
||
| 331 | }; |
||
| 332 | |||
| 333 | Gui.prototype.getHeight = function() { |
||
| 334 | return exports.innerHeight - $('header').height(); |
||
| 335 | }; |
||
| 336 | |||
| 337 | Gui.prototype.resize = function() { |
||
| 338 | this.svg.attr('width', this.getWidth()); |
||
| 339 | this.svg.attr('height', this.getHeight()); |
||
| 340 | }; |
||
| 341 | |||
| 342 | Gui.prototype.drawEvent = function(event) { |
||
| 343 | if (document.hidden) { |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | |||
| 347 | new Event(event, this.svg).draw(this.getWidth(), this.getHeight()); |
||
| 348 | |||
| 349 | // Remove HTML of decayed events |
||
| 350 | // Keep it less than 50 |
||
| 351 | let $area = $('#area'); |
||
| 352 | if($area.find('svg g').length > 50){ |
||
| 353 | $area.find('svg g:lt(10)').remove(); |
||
| 354 | } |
||
| 355 | }; |
||
| 356 | |||
| 357 | return Gui; |
||
| 358 | }()); |
||
| 359 | |||
| 621 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.